class FatherController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        [fatherInstanceList: Father.list(params), fatherInstanceTotal: Father.count()]
    }

    def create = {
        def fatherInstance = new Father()
        fatherInstance.properties = params
        return [fatherInstance: fatherInstance]
    }

    def save = {
        def fatherInstance = new Father(params)
        if (fatherInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'father.label', default: 'Father'), fatherInstance.id])}"
            redirect(action: "show", id: fatherInstance.id)
        }
        else {
            render(view: "create", model: [fatherInstance: fatherInstance])
        }
    }

    def show = {
        def fatherInstance = Father.get(params.id)
        if (!fatherInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'father.label', default: 'Father'), params.id])}"
            redirect(action: "list")
        }
        else {
            [fatherInstance: fatherInstance]
        }
    }

    def edit = {
        def fatherInstance = Father.get(params.id)
        if (!fatherInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'father.label', default: 'Father'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [fatherInstance: fatherInstance]
        }
    }

    def update = {
        def fatherInstance = Father.get(params.id)
        if (fatherInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (fatherInstance.version > version) {
                    
                    fatherInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'father.label', default: 'Father')] as Object[], "Another user has updated this Father while you were editing")
                    render(view: "edit", model: [fatherInstance: fatherInstance])
                    return
                }
            }
			println "---- params: $params ---"
			println "---- 1 before ----"
			fatherInstance.children.each {println it}
			
			fatherInstance.properties = params
			
			println "---- 2 after ----"
			fatherInstance.children.each {println it}
            if (!fatherInstance.hasErrors() && fatherInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'father.label', default: 'Father'), fatherInstance.id])}"
                redirect(action: "show", id: fatherInstance.id)
            }
            else {
                render(view: "edit", model: [fatherInstance: fatherInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'father.label', default: 'Father'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def fatherInstance = Father.get(params.id)
        if (fatherInstance) {
            try {
                fatherInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'father.label', default: 'Father'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'father.label', default: 'Father'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'father.label', default: 'Father'), params.id])}"
            redirect(action: "list")
        }
    }
}
